package com.douban.crawl.db;

import java.sql.*;

public class JDBCUtils {
    // 数据库驱动类，注意新版和老版有区别
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    // test替换为自己的数据库
    private static final String URL = "jdbc:mysql://localhost:3306/douban_crawl?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    /**
     * 连接资源
     * @return
     * @throws Exception
     */
    public static Connection getConn() throws Exception {
        Class.forName(DRIVER);
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    /**
     * 关闭资源
     * @param conn
     * @param ps
     */
    public static void closeRes(Connection conn, PreparedStatement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭资源
     * @param conn
     * @param ps
     * @param rs
     */
    public static void closeRes(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

